#include<iostream>
#include<unistd.h>
#include<sys/fcntl.h>

bool SetNonBlock(int fd)
{
    int fl = fcntl(fd,F_GETFL);
    if(fl<0)
        return false;
    fcntl(fd,F_SETFL,fl|O_NONBLOCK);
    return true;
}

int main()
{
    SetNonBlock(0);
    char buffer[256];
    while(true)
    {
        sleep(1);
        errno = 0;
        ssize_t s = read(0,buffer,(sizeof buffer)-1);
        if(s>0)
        {
            buffer[s] = 0;
            std::cout<<"echo# "<<buffer<<std::endl;
        }
        else if(errno==EINTR)
        {
            std::cout << "当前IO可能被信号中断，在试一试吧" << std::endl;
            continue;
        }
        else if(errno == EWOULDBLOCK || errno == EAGAIN)
        {
            std::cout << "当前0号fd数据没有就绪, 请下一次再来试试吧" << std::endl;
            continue;
        }
        else
        {
            //worry
        }
    }
    return 0;
}